package com.neu.admin;

import com.neu.service.ShoppingOrderService;
import com.neu.util.response.BaseResponse;
import com.neu.vo.ShoppingOrder;
import com.neu.vo.ShoppingOrderGoods;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by yinjian by 2019/11/25
 * 订单管理 尹健
 */
@RestController
@RequestMapping("/admin/order")
public class AdminOrderController {

    @Autowired
    private ShoppingOrderService shoppingOrderService;

    /* *
     * 作用:获取订单列表信息
        {
            "pagenum":1,
            "pagesize":2,
            "userId":123,
            "orderSn":"11111111",
            "orderStatuslist":[4,5]
        }
     * yinjian on 2019/11/25
     * @param ShoppingOrder
     **/
    @ApiOperation("获取订单列表信息")
    @PostMapping("/list")
    private Object list(@RequestBody ShoppingOrder ShoppingOrder){
        if (null == ShoppingOrder.getPagenum() || "".equals(ShoppingOrder.getPagenum())){
            return BaseResponse.badArgument();
        }
        if (null == ShoppingOrder.getPagesize() || "".equals(ShoppingOrder.getPagesize())){
            return BaseResponse.badArgument();
        }
        return BaseResponse.generateOKResponseEntity(shoppingOrderService.list(ShoppingOrder));
    }

    /* *
     * 作用:查看订单详情
        {
            "orderId":1
        }
     * yinjian on 2019/11/26
     * @param shoppingOrderGoods
     **/
    @ApiOperation("查看订单详情")
    @PostMapping("/detail")
    private Object detail(@RequestBody ShoppingOrderGoods shoppingOrderGoods){
        if (null == shoppingOrderGoods.getOrderId() || "".equals(shoppingOrderGoods.getOrderId())){
            return BaseResponse.badArgument();
        }
        return BaseResponse.generateOKResponseEntity(shoppingOrderService.detail(shoppingOrderGoods));
    }

    /* *
     * 作用:订单发货
        {
            "orderSn":321312,
            "shipChannel":"超级快递公司",
            "shipSn":"1111111"
        }
     * yinjian on 2019/11/27
     * @param shoppingOrder
     **/
    @ApiOperation("订单发货")
    @PostMapping("/ship")
    private Object ship(@RequestBody ShoppingOrder shoppingOrder){
        if (null == shoppingOrder.getOrderSn() || "".equals(shoppingOrder.getOrderSn())){
            return BaseResponse.badArgument();
        }
        if (null == shoppingOrder.getShipSn() || "".equals(shoppingOrder.getShipSn())){
            return BaseResponse.badArgument();
        }
        if (null == shoppingOrder.getShipChannel() || "".equals(shoppingOrder.getShipChannel())){
            return BaseResponse.badArgument();
        }
        int code = shoppingOrderService.updateWithOptimisticLocker(shoppingOrder);
        if (code ==1){
            return BaseResponse.generateOKResponseEntity("发货成功");
        }
        return BaseResponse.generateOKResponseEntity("发货失败");
    }

    /* *
     * 作用:订单退款
     {
     "orderSn"：11111111
      }
     * yinjian on 2019/11/27
     * @param shoppingOrder
     **/
    @ApiOperation("订单退款")
    @PostMapping("/refund")
    private Object refund(@RequestBody ShoppingOrder shoppingOrder){
        if (null == shoppingOrder.getOrderSn() || "".equals(shoppingOrder.getOrderSn())){
            return BaseResponse.badArgument();
        }
        int code = shoppingOrderService.queryByOid(shoppingOrder);
        if (code ==1){
            return BaseResponse.generateOKResponseEntity("退款成功");
        }
        return BaseResponse.generateOKResponseEntity("退款失败");
    }
}
